package org.qengine.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qengine.dao.PaperInfoDao;
import org.qengine.dao.UserAnswersDao;
import org.qengine.entity.DataDictEntity;
import org.qengine.entity.PaperInfoEntity;
import org.qengine.entity.UserAnswersEntity;
import org.qengine.vo.UserAnswerVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.qengine.service.QuestionService;
import org.qengine.entity.PapersEntity;
import org.qengine.dao.PapersDao;
import org.qengine.entity.PaperInfoEntity;
import org.qengine.entity.QuestionEntity;

@Service("userAnswerService")
public class UserAnswersServiceImpl extends ServiceImpl<UserAnswersDao, UserAnswersEntity> implements UserAnswersService  {
    @Autowired
    QuestionService questionService;

    @Autowired
    PapersDao papersDao;


    QuestionEntity questionEntity=new QuestionEntity();

    @Override
    public Integer add(UserAnswersEntity answer) {

        return baseMapper.insert(answer);
    }

    @Override
    public Integer delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Integer getByUserId(Long id) {

        return 0;
    }

    @Override
    public IPage<UserAnswersEntity> search(UserAnswerVo userAnswerVo) {
        QueryWrapper<UserAnswersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userAnswerVo.getUserId());


        // 获取分页参数，默认第一页，每页 10 条记录
        int curPage = userAnswerVo.getCurPage() != null && userAnswerVo.getCurPage() > 0 ? userAnswerVo.getCurPage() : 1;
        int pageline = userAnswerVo.getPageline() != null && userAnswerVo.getPageline() > 0 ? userAnswerVo.getPageline() : 10;

        // 创建 Page 对象
        Page<UserAnswersEntity> page = new Page<>(curPage, pageline);

        // 执行分页查询
        return baseMapper.selectPage(page,queryWrapper);
    }



    @Override
    public String stateList( UserAnswersEntity answer) {
        //正误状态列表
        String list = "";

        String[] ansArray =  (String[]) answer.getAnswers().split(",");//学生答案的数组

        Long paperId = answer.getPaperId().longValue();//获取试卷ID
        Long[] questionIds = papersDao.findById(paperId).getQuestionIdsAsArray();//调用接口，通过试卷ID得到题号的数组

        for(int i=0;i<ansArray.length;i++){//本张试卷的第i题
            String stuAnswer = ansArray[i];//学生第i题的答案

            Integer questionId = questionIds[i].intValue();//第i题的题号
            questionEntity = questionService.getQuestionById(questionId);//通过题号获取实体类
            Integer  typeId = questionEntity.getTypeID();//通过实体类查到该题的题型
            String rightAnswer = null;
            if(typeId==1){
                rightAnswer =""+questionService.findStemAndAnswerById(questionId).get(1).charAt(0);//单选，获取第一个字符
            }
            else if(typeId==2){
                String answerString = questionService.findStemAndAnswerById(questionId).get(1);
                char target ='@';
                rightAnswer =""+answerString.charAt(0);//多选，先添加第一个正确答案
                for(int j =1;j<answerString.length();j++){
                    if(target==answerString.charAt(j)){//遇到@符号，则把后一个字符加入正确答案列表
                        rightAnswer += answerString.charAt(j+1);
                    }
                }
            }
            else{
                rightAnswer =questionService.findStemAndAnswerById(questionId).get(1);//直接获取正确答案即可
            }

            if(stuAnswer.equals(rightAnswer)){
                list += "1";
            }
            else {
                list += "0";
            }
        }
        return list;
    }

    @Override
    public UserAnswersEntity getStuAnswersByStuIDAndPaperID(Long stuID, Long paperID) {
        // 构造查询条件
        QueryWrapper<UserAnswersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", stuID)
                    .eq("paper_id", paperID);

        // 执行查询并返回结果
        return baseMapper.selectOne(queryWrapper);
    }


}
